// pages/addpost/addpost.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    photo: "/images/icon/chooseimage.png",
    logged: false,
    userInfo: {},
    title: "",
    submit:"submit",
    head_height:0,
    clientHeight:0
  },

  // 初始化编辑器
  onEditorReady() {
    var that = this
    wx.createSelectorQuery().select('#editor').context(function(res) {
      that.editorCtx = res.context
    }).exec()
  },

  testsub:function() {
    var that = this
    that.editorCtx.getContents({
      success: function(res) {
        console.log(res.html)
      }
    })
  },

  chooseImages: function () {
    this.setData({
      logged: true,
      userInfo: getApp().globalData.userInfo,
    })
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res)
        const tempFilePath = res.tempFilePaths[0]
        that.setData({
          photo: tempFilePath
        })
      }
    })
  },

  back: function () {
    wx.navigateBack({
      delta: 1,
    })
  },

  submit: function () {
    var that = this
    if (this.data.photo == "/images/icon/chooseimage.png") {
      //验证头图
      wx.showToast({
        title: '请选择图片',
        icon: 'error',
        duration: 2000
      })
    } 
    else if(that.data.title == "") {
      //验证标题
      wx.showToast({
        title: '请输入标题',
        icon: 'error',
        duration: 2000
      })
    }
    else {
      that.editorCtx.getContents({
        success: function(res) {
          console.log(res.html)
          var maintext_2 = res.html
          wx.showLoading({
            title: '正在发布',
          })
//数据库操作
          that.setData({
            submit:""
          })
          var timestamp = new Date().getTime()
          //var maintext_2 = that.data.maintext
          wx.cloud.uploadFile({
            cloudPath: 'discovery/' + timestamp + '.jpg',
            filePath: that.data.photo, // 文件路径
            success: res => {
              // get resource ID
              console.log(res.fileID)
              wx.cloud.callFunction({
                name: 'addpost',
                data: {
                  title: that.data.title,
                  maintext: maintext_2,
                  photo: res.fileID,
                  timestamp: timestamp
                },
                success: res => {
                  console.log(res)
                  wx.hideLoading()
                  wx.showToast({
                    title: '发布成功',
                    icon: 'success',
                    duration: 1000
                  })
                  setTimeout(function () {
                    that.back()
                  }, 1000)
                },
                fail: err => {
                  console.log(err)
                  that.setData({
                    submit:"submit"
                  })
                }
              })
            },
            fail: err => {
              // handle error
              that.setData({
                submit:"submit"
              })
            }
          })
        }
      })
    }
  },

  getTitle: function (e) {
    //console.log(e.detail.value)
    this.setData({
      title: e.detail.value
    })
  },

  back: function () {
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.chooseImages()

        /* 获取窗口高度 */
        var that = this
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              clientHeight: res.windowHeight,
            });
          }
        })

    var query = wx.createSelectorQuery();
    query.select('.topbar').boundingClientRect(function (rect) {
      that.setData({
        head_height: rect.height
      })
    }).exec();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})